Post Selector all generators

The selector field on a generator allows an ApplciationSet to post-filter results using the Kubernetes common labelSelector format and the generated values.

matchLabels is a map of {key,value} pairs. This list generator generates a set of two Applications, which is then filtered using matchLabels to only the list element containing the key env with value staging:

  1. spec:
  2. generators:
  3. - list:
  4. elements:
  5. - cluster: engineering-dev
  6. url: https://kubernetes.default.svc
  7. env: staging
  8. - cluster: engineering-prod
  9. url: https://kubernetes.default.svc
  10. env: prod
  11. selector:
  12. matchLabels:
  13. env: staging

The list generator + matchLabels selector generates a single set of parameters:

  1. - cluster: engineering-dev
  2. url: https://kubernetes.default.svc
  3. env: staging

It is also possible to use matchExpressions for more powerful selectors.

A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is the “key”, the operator is “In”, and the values array contains only the “value”. So the same example using matchExpressions looks like:

  1. spec:
  2. generators:
  3. - list:
  4. elements:
  5. - cluster: engineering-dev
  6. url: https://kubernetes.default.svc
  7. env: staging
  8. - cluster: engineering-prod
  9. url: https://kubernetes.default.svc
  10. env: prod
  11. selector:
  12. matchExpressions:
  13. - key: env
  14. operator: In
  15. values:
  16. - staging

Valid operators include In, NotIn, Exists, and DoesNotExist. The values set must be non-empty in the case of In and NotIn.

Full Example

In the example, the list generator generates a set of two applications, which then filter by the key value to only select the env with value staging:

  1. apiVersion: argoproj.io/v1alpha1
  2. kind: ApplicationSet
  3. metadata:
  4. name: guestbook
  5. spec:
  6. goTemplate: true
  7. goTemplateOptions: ["missingkey=error"]
  8. generators:
  9. - list:
  10. elements:
  11. - cluster: engineering-dev
  12. url: https://kubernetes.default.svc
  13. env: staging
  14. - cluster: engineering-prod
  15. url: https://kubernetes.default.svc
  16. env: prod
  17. selector:
  18. matchLabels:
  19. env: staging
  20. template:
  21. metadata:
  22. name: '{{.cluster}}-guestbook'
  23. spec:
  24. project: default
  25. source:
  26. repoURL: https://github.com/argoproj-labs/applicationset.git
  27. targetRevision: HEAD
  28. path: examples/list-generator/guestbook/{{.cluster}}
  29. destination:
  30. server: '{{.url}}'
  31. namespace: guestbook